package com.leyou.auth.task;

import com.leyou.auth.config.JwtProperties;
import com.leyou.auth.entity.AppInfo;
import com.leyou.auth.mapper.ApplicationInfoMapper;
import com.leyou.common.auth.utils.JwtUtils;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Appinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.AstUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.persistence.Id;
import java.util.List;

/**
 * @package com.leyou.auth.task
 * @description:
 * @author: 86157
 * @date 2019/7/9 19:23
 * @version: V1.0
 */
@Slf4j
@Component
public class PrivilegeTokenHolder {
    @Autowired
    private JwtProperties prop;

    private String tooken;

    @Autowired
    private ApplicationInfoMapper infoMapper;


    /**
     * token刷新间隔
     */
    private static final long TOKEN_REFRESH_INTERVAL = 86400000L;

    @Scheduled(fixedDelay = TOKEN_REFRESH_INTERVAL)
    public void loadTooken() {
        try {
            //查询权限服务列表
            List<Long> longs = infoMapper.queryTargetIdList(prop.getApp().getId());
            //封装载荷
            AppInfo appInfo = new AppInfo();
            appInfo.setTargetList(longs);
            appInfo.setId(prop.getApp().getId());
            appInfo.setServiceName(prop.getApp().getSecret());
            //生产tooken
            JwtUtils.generateTokenExpireInMinutes(appInfo, prop.getPrivateKey(), prop.getApp().getExpire());

            log.info("【网关】定时获取token成功");
        } catch (Exception e) {
            log.info("【网关】定时获取token失败");
        }

    }

    public String getTooken() {
        return tooken;
    }
}